package com.isczy.utils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.Map;

/********************************************
 * @Brief: 统一返回结果集
 * @Version: 1.0
 * @Date: 2020年12月25日 16:46:46
 * @Author: ChangZiYang
 * ******************************************
 */
@Data
public class DataResult {

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "响应码")
    private Integer code;

    @ApiModelProperty(value = "响应消息")
    private String message;

    @ApiModelProperty(value = "响应数据")
    private Object data;

    //把构造方法私有
    private DataResult() {}

    public static DataResultBuilder builder() {
        return new DataResultBuilder();
    }
    public static class DataResultBuilder{
        Map<String,Object> resultMap = new HashMap<>();
        private Boolean success;
        private Integer code;
        private String message;
        private Object data = resultMap;

        public DataResultBuilder success(Boolean success) {
            this.success = success;
            return this;
        }
        public DataResultBuilder code(int code) {
            this.code = code;
            return this;
        }
        public DataResultBuilder message(String message) {
            this.message = message;
            return this;
        }
        public DataResultBuilder data(Object data) {
            this.data = data;
            return this;
        }
        public DataResultBuilder data(String key, Object value) {
            this.resultMap.put(key, value);
            return this;
        }
        public DataResult successBuild() {
            DataResult dataResult = new DataResult();
            dataResult.setSuccess(success == null ? true : success);
            dataResult.setCode(code == null ? ResultCode.SUCCESS : code);
            dataResult.setMessage(message == null ? "成功" : message);
            dataResult.setData(data);
            return dataResult;
        }
        public DataResult errorBuild() {
            DataResult dataResult = new DataResult();
            dataResult.setSuccess(success == null ? false : success);
            dataResult.setCode(code == null ? ResultCode.ERROR : code);
            dataResult.setMessage(message == null ? "失败" : message);
            dataResult.setData(data);
            return dataResult;
        }
    }

}
